package com.example.sbeducation.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.sbeducation.common.Result;
import com.example.sbeducation.dto.HomeworkDTO;
import com.example.sbeducation.dto.HomeworkSubmissionDTO;
import com.example.sbeducation.dto.HomeworkReviewDTO;
import com.example.sbeducation.entity.Homework;
import com.example.sbeducation.service.HomeworkService;
import com.example.sbeducation.service.HomeworkSubmissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 作业控制器
 */
@RestController
@RequestMapping("/api/homework")
public class HomeworkController {

  @Autowired
  private HomeworkService homeworkService;

  @Autowired
  private HomeworkSubmissionService submissionService;

  /**
   * 创建作业
   */
  @PostMapping
  public Result<Long> createHomework(@Valid @RequestBody HomeworkDTO homeworkDTO) {
    Long homeworkId = homeworkService.createHomework(homeworkDTO);
    return Result.success(homeworkId);
  }

  /**
   * 更新作业
   */
  @PutMapping
  public Result<Void> updateHomework(@Valid @RequestBody HomeworkDTO homeworkDTO) {
    homeworkService.updateHomework(homeworkDTO);
    return Result.success("更新成功");
  }

  /**
   * 删除作业
   */
  @DeleteMapping("/{homeworkId}")
  public Result<Void> deleteHomework(@PathVariable Long homeworkId) {
    homeworkService.deleteHomework(homeworkId);
    return Result.success("删除成功");
  }

  /**
   * 获取作业详情
   */
  @GetMapping("/{id}")
  public Result<HomeworkDTO> getHomeworkDetail(@PathVariable Long id) {
    HomeworkDTO homeworkDTO = homeworkService.getHomeworkDetail(id);
    return Result.success(homeworkDTO);
  }

  /**
   * 获取教师的作业列表
   */
  @GetMapping("/teacher")
  public Result<Page<Homework>> getTeacherHomeworks(
      @RequestParam Long teacherId,
      @RequestParam(required = false) String title,
      @RequestParam(defaultValue = "1") Integer page,
      @RequestParam(defaultValue = "10") Integer size) {
    Page<Homework> homeworkPage = homeworkService.getTeacherHomeworkList(teacherId, title, page, size);
    return Result.success(homeworkPage);
  }

  /**
   * 获取学生的作业列表
   */
  @GetMapping("/student")
  public Result<Page<HomeworkDTO>> getStudentHomeworks(
      @RequestParam Long studentId,
      @RequestParam(required = false) String title,
      @RequestParam(defaultValue = "1") Integer page,
      @RequestParam(defaultValue = "10") Integer size) {
    Page<HomeworkDTO> homeworkPage = homeworkService.getStudentHomeworkList(studentId, title, page, size);
    return Result.success(homeworkPage);
  }

  /**
   * 获取作业提交列表
   */
  @GetMapping("/{homeworkId}/submissions")
  public Result<List<HomeworkSubmissionDTO>> getSubmissionsByHomeworkId(@PathVariable Long homeworkId) {
    List<HomeworkSubmissionDTO> submissions = submissionService.getSubmissionsByHomeworkId(homeworkId);
    return Result.success(submissions);
  }

  /**
   * 提交批改结果
   */
  @PostMapping("/review")
  public Result<Boolean> reviewSubmission(@RequestBody HomeworkReviewDTO reviewDTO) {
    boolean result = submissionService.reviewSubmission(reviewDTO);
    return Result.success(result);
  }
}